#include "pch.hpp"
#include "Window.hpp"

using namespace Engine;
using namespace Engine::Ui;




Window::Window(Widget::weak_ptr parent)
	: Frame(parent)
	, m_resizingBorder(Border::Center)
{
	stateChanged.connect(boost::bind(&Window::onStateChanged, this, _1));
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Window::post_create(Widget::ptr _this)
{
	__super::post_create(_this);

	m_title = Text::create("Window", _this);
	add(m_title);

	onStateChanged(_this);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




intsize2 Window::configuredMinimumSize() const
{
	auto minSize  = __super::configuredMinimumSize();
	auto textSize = m_title ? m_title->size() : intsize2(0, 0);
	auto pad      = padding();

	// For now, the minimum size depends on the width of the window title.
	// However in the future the width might increase further because of buttons on the title bar.
	minSize.width = std::max(minSize.width, textSize.width + pad.x + pad.z);

	return minSize;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

bool Window::resizing() const
{
	return m_resizingBorder != Border::Center;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




QString Window::title() const
{
	return m_title->text();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Window::setTitle(const QString& title)
{
	m_title->setText(title);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




Widgets::Type Window::type() const
{
	return Widgets::Window;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Window::onStateChanged(Widget::weak_ptr that)
{
	auto ptr = that.lock();
	if(ptr && m_title && this == ptr.get())
	{
		// The frame's geometry might have changed:
		// reposition the title text
		m_title->setPos(int2(0, -padding().y));
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Window::update(NestedState& state)
{
	Frame::update(state);
	const Cursor& cursor = state.cursor();

	if(mousePressed() && hoveredBorder() == Border::Top)
	{
		// As long as the mouse is placed over the window's title bar,
		// we have to move it with the mouse
		if(!state.isAttached(weak_from_this()) && !state.hasAttached())
			state.attach(weak_from_this());
	}
	else
	{
		if(state.isAttached(weak_from_this()))
			state.detach(weak_from_this());
	}


	if(mousePressed() && hoveredBorder() != Border::Center && hoveredBorder() != Border::Top)
	{
		if(!state.isAttachedBorder(weak_from_this()) && !state.hasAttached())
			state.attachBorder(weak_from_this(), hoveredBorder());
	}
	else if(!cursor.keyPressed(Key::MouseLeft))
	{
		if(state.isAttachedBorder(weak_from_this()))
			state.detachBorder(weak_from_this());
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
